package com.tyqx.hashchainutil.common;

import com.tyqx.hashchainutil.org.app.client.CAClient;
import com.tyqx.hashchainutil.org.app.config.Config;
import com.tyqx.hashchainutil.org.app.user.UserContext;
import com.tyqx.hashchainutil.org.app.util.Util;

import java.util.Properties;

public class CreateWallet {

    public CAClient initCAClient(String caUrl, String caTlsFilePath) throws Exception{
        Properties properties = Util.setTlsProperties(caTlsFilePath);
        CAClient caClient = new CAClient(caUrl, properties);
        return caClient;
    }

    public UserContext enrollAdminUserTLS() throws Exception {
        String caUrl = "https://" + Config.CA_TLS_URL_2 + ":8056";
        String caTlsFilePath = Config.CA_TLS_TLS_FILE_2;
        CAClient caClient = initCAClient(caUrl, caTlsFilePath);

        // Enroll Admin to Org1MSP
        UserContext adminUserContext = new UserContext();
        adminUserContext.setName(Config.CA_TLS_ADMIN_2);
        adminUserContext.setAffiliation(Config.ORG_AFFI_1);
        adminUserContext.setMspId(Config.ORG_MSP_1);
        caClient.setAdminUserContext(adminUserContext);
        adminUserContext = caClient.enrollAdminUserTLS(Config.CA_TLS_ADMIN_2, Config.CA_TLS_ADMIN_PASSWORD_2);

        return adminUserContext;
    }

    public String registerUser (String username, String organization) throws Exception{
        UserContext adminUserContext = Util.readUserContext(Config.ORG_AFFI_1, Config.CA_MSP_ADMIN_2);
        String caUrl = "https://" + Config.CA_MSP_URL_2 + ":7056";
        String caTlsFilePath = Config.CA_MSP_TLS_FILE_2;
        CAClient caClient = initCAClient(caUrl, caTlsFilePath);
        caClient.setAdminUserContext(adminUserContext);
        String enrollmentSecret = caClient.registerUser(username, organization);
        return enrollmentSecret;
    }




}
